var memberServiceNative = require("../services/memberService.js");
var storeServiceNative = require('../services/storeService');
var memberVerifyCodeServiceNative = require('../services/MemberVerifyCodeService');
var errorHandler = require('../common/errorHandler');
var idHelper = require("../common/utils/id-helper");
var async = require("async");
var moment = require("moment");
function getRandomCode() {
	return Math.ceil(Math.random() * 1000000000) + '';
}
var MemberVerifyCodeController = {
	createNew: function (req, res, next) {
		var storeService = errorHandler.errorToResponse(req, res, storeServiceNative);
		var memberService = errorHandler.errorToResponse(req, res, memberServiceNative);
		var memberVerifyCodeService = errorHandler.errorToResponse(req, res, memberVerifyCodeServiceNative);
		new Promise(function (resolve) {
			storeService.getStoreById(req.params.storeId, '', function (err, store) {
				if (err || !store) {
					if (!store) {
						err = '参数错误';
					}
					return res.status(500).send(err);
				}
				resolve(store);
			})
		}).then(function (store) {
				return new Promise(function (resolve) {
					memberService.getMemberById(req.params.memberId, function (err, member) {
						if (err || !member) {
							if (!member) {
								err = '参数错误';
							}
							return res.status(500).send(err);
						}
						var data = {store: store, member: member};
						resolve(data);
					})
				})

			}).then(function (data) {
				var firstCode = getRandomCode();
				var firstLength = firstCode.length;
				if (firstLength != 9) {
					for (var i = 0; i < 9 - firstLength; i++) {
						firstCode += '0';
					}
				}
				var secondCode = getRandomCode();
				var secondLength = secondCode.length;
				if (secondLength != 9) {
					var s = '';
					for (var i = 0; i < 9 - secondLength; i++) {
						s += '0';
					}
					secondCode = s + secondCode;
				}

				var code = firstCode + '' + secondCode;
				var verifyCode = {
					id: idHelper.createId(),
					expired_time: moment().format('YYYY-MM-DD HH:mm:ss'),
					member_id: data.member.id,
					store_id: data.store.id,
					verify_code: code
				};
				memberVerifyCodeService.createNew(verifyCode, function (err, data) {
					if (err) {
						return res.status(500).send(err);
					}
					return res.send(verifyCode.verify_code);
				})

			}
		);
	},
	getPayment: function (req, res, next) {
		var storeId = req.params.storeId;
		var memberId = req.params.memberId;
		var verifyCode = req.query.verifyCode;
		var storeService = errorHandler.errorToResponse(req, res, storeServiceNative);
		var memberService = errorHandler.errorToResponse(req, res, memberServiceNative);
		var memberVerifyCodeService = errorHandler.errorToResponse(req, res, memberVerifyCodeServiceNative);
		new Promise(function (resolve) {
			storeService.getStoreById(storeId, '', function (err, store) {
				if (err || !store) {
					if (!store) {
						err = '参数错误';
					}
					return res.status(500).send(err);
				}
				resolve(store);
			})
		}).then(function (store) {
				return new Promise(function (resolve) {
					memberService.getMemberById(memberId, function (err, member) {
						if (err || !member) {
							if (!member) {
								err = '参数错误';
							}
							return res.status(500).send(err);
						}
						var data = {store: store, member: member};
						resolve(data);
					})
				})

			}).then(function (dataObj) {
				memberVerifyCodeService.getPayment(verifyCode, storeId, memberId, function (err, data) {
					if (err) {
						return res.status(500).send(err);
					}
					res.send(data);
				})
			});
	}
}


module.exports = MemberVerifyCodeController;